package com.igeek.clud_system.controller;

import com.igeek.clud_system.common.Const;
import com.igeek.clud_system.entity.ClubMembers;
import com.igeek.clud_system.entity.Student;
import com.igeek.clud_system.pojo.ResponseResult;
import com.igeek.clud_system.service.StudentService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class StudentController {
    @Autowired
    StudentService studentService;


    @RequestMapping("student/{studentId}/{password}")
    public ResponseResult getStrudent(@PathVariable("studentId")Integer studentId, @PathVariable("password")String password, HttpSession session){
        Student student =studentService.findStudentById(studentId);
        //查询用户信息
        ResponseResult result = new ResponseResult();
        if(student != null){
            System.out.println("用户名存在");
            if(student.getPassword().equals(password)){
                System.out.println("登录成功");
                System.out.println(student.getStudentId()+"/"+student.getStudentName()+"/"+student.getPassword()+"/"+student.getHead());
                //将用户进行会话保存
                session.setAttribute(Const.LOGIN_USER, student);
                result.setState(200);
            }
            else{
                result.put("pwdError", "密码错误");
            }
        }
        else{
            System.out.println("用户名不存在");
            result.put("nameError", "用户名不存在");
        }
        return result;
    }
    @GetMapping("personal")
    public Student  getPersonal(HttpSession session){
        Student student=(Student) session.getAttribute(Const.LOGIN_USER);
        if(student!=null){
            System.out.println(student.getActivityScore()+"进入");
            return student;
        }else{
            System.out.println("未登录");
            return null;
        }
    }
    @RequestMapping("updateStudentHead/{head}")
    public void updateStudentHead(@PathVariable("head")String head,HttpSession session){
        Student student=(Student) session.getAttribute(Const.LOGIN_USER);
        studentService.updateStudentHead(head,student.getStudentId());
    }
    @RequestMapping("updateStudentPassword/{password}")
    public void updatePassword(@PathVariable("password")String password,HttpSession session){
        Student student=(Student) session.getAttribute(Const.LOGIN_USER);
        System.out.println(student.getStudentId()+"/"+student.getStudentName()+"/"+student.getPassword()+"/"+student.getHead());
        studentService.updateStudentPassword(password,student.getStudentId());
    }


}
